package com.spring.utils;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author SpringWang
 * @date 2021/5/20
 */
public class ConnectionUtils {
    private ConnectionUtils() {
    }

    private static final ConnectionUtils connectionUtils = new ConnectionUtils();

    private final ThreadLocal<Connection> threadLocal = new ThreadLocal<>();

    public static ConnectionUtils getInstance() {
        return connectionUtils;
    }

    public Connection getCurrentThreadConn() throws SQLException {
        // 判断当前线程是否已绑定connection
        Connection connection = threadLocal.get();
        if (connection == null) {
            connection = DruidUtils.getInstance().getConnection();
            threadLocal.set(connection);
        }
        return connection;
    }
}
